﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using TouchRC.BarManagers;
using TouchRC.Classes;
using TouchRC.Custom_Controls;
using Microsoft.Drawing;
using TouchRC.Logic;
using System.Drawing;
using Fluid.Controls;

namespace TouchRC
{
    public class KeyboardPanel : FluidPanelBase
    {
        #region Private Members

        private List<RemoteControlButton> m_Buttons;
        private RemoteControlButton m_GreenButton;
        private static KeyboardPanel m_Instance;

        private bool m_IsAlreadyVisible = false;
        private ScreenOrientation m_CurrentlyDisplayedOrientation = ScreenOrientation.Landscape;

        #endregion

        public KeyboardPanel()
        {
            //base.ShowBackground = false;
            EnableRemoteControlGesture = false;
            int widescreenAdjustment = (AppSettings.ScreenResolution.Height - 320) / 2;
        }

        public void ShowKeyboard()
        {
            if (!m_IsAlreadyVisible)
            {
                m_IsAlreadyVisible = true;
                if (m_CurrentlyDisplayedOrientation != AppSettings.CurrentOrientation)
                {
                    m_Instance = new KeyboardPanel();
                    m_Instance.ShowKeyboard();
                    return;
                }
                Show(ShowTransition.FromLeft);
            }
        }

        public void HideKeyboard()
        {
            if (m_IsAlreadyVisible)
            {
                m_IsAlreadyVisible = false;
                Close(ShowTransition.FromRight);
            }
        }

        public static KeyboardPanel Instance
        {
            get
            {
                if (m_Instance == null) m_Instance = new KeyboardPanel();
                return m_Instance;
            }
        }

        protected override void InitControl()
        {
            OriginalLeft = 0;
            OriginalTop = 0;

            ShowHeader = false;
            ShowMenuButton = false;
            
            m_Instance = this;
            m_Buttons = new List<RemoteControlButton>();
            m_GreenButton = new RemoteControlButton(RemoteCommands.GoToPlaces.Start,
                ImageFactory.GetMediaImage(MediaIcons.GreenButton));

            base.InitControl();
        }

        public override void SetControlPositions(ScreenOrientation screenOrientation)
        {
            m_CurrentlyDisplayedOrientation = screenOrientation;

            List<RemoteControlButton> sortedButtons;
            int startingX;
            int startingY;
            int maxWidth;

            int imageSize; ;
            int margin = 5;

            int maxDiffX;
            int maxDiffY;

            if (AppSettings.IsWidescreen)
            {
                maxDiffX = 220;
                maxDiffY = 355;
                imageSize = 40;
            }
            else
            {
                imageSize = 35;
                maxDiffX = 200;
                maxDiffY = 320;
            }

            int greenButtonDim = imageSize * 2;
            double greenButtonX;
            int greenButtonY;

            if (screenOrientation == ScreenOrientation.Landscape)
            {
                sortedButtons = m_Buttons.OrderBy(x => x.LandscapePos).ToList();
                startingX = (AppSettings.ScreenResolution.Width - maxDiffY) / 2;
                startingY = (AppSettings.ScreenResolution.Height - maxDiffX) / 2;
                maxWidth = maxDiffY + startingX;
                greenButtonX = startingX + (4.5 * imageSize) + (3.5 * margin) - (imageSize);
                greenButtonY = startingY + (3 * imageSize) + (2 * margin) - (imageSize);
            }
            else
            {
                sortedButtons = m_Buttons.OrderBy(x => x.PortraitPos).ToList();
                startingX = (AppSettings.ScreenResolution.Width - maxDiffX) / 2;
                maxWidth = maxDiffX + startingX;
                startingY = (AppSettings.ScreenResolution.Height - maxDiffY) / 2;
                m_GreenButton.Bounds = new Rectangle();
                greenButtonX = startingX + (2.5 * imageSize) + (1.5 * margin) - (imageSize);
                greenButtonY = startingY + (6 * imageSize) + (5 * margin) - (imageSize);
            }

            int width = maxWidth;

            m_GreenButton.Bounds = new Rectangle(m_GreenButton.ScaleX((int)greenButtonX), m_GreenButton.ScaleY(greenButtonY),
                m_GreenButton.ScaleX(greenButtonDim), m_GreenButton.ScaleY(greenButtonDim));

            int currX = startingX;
            int currY = startingY;

            foreach (RemoteControlButton button in sortedButtons)
            {
                if (currX + imageSize > width)
                {
                    currX = startingX;
                    currY += imageSize + margin;
                }

                button.Bounds = new Rectangle(button.ScaleX(currX), button.ScaleY(currY),
                    button.ScaleX(imageSize), button.ScaleY(imageSize));
                currX += imageSize + margin;
            }
        }

        public override void RefreshValues()
        {
        }

        public override void AddControls()
        {
            Controls.Add(m_GreenButton);
            foreach (RemoteControlButton button in m_Buttons)
            {
                if (!(button.Enum is Skip))
                {
                    Controls.Add(button);
                }
            }
        }

        public override void InitPanelControls()
        {
            Skip skip = new Skip();

            m_Buttons.Add(new RemoteControlButton(RemoteControlSpecial.RemoveRemote,
                ImageFactory.GetMediaImage(MediaIcons.close), 0, 0));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.GoToPlaces.MyVideos,
                            ImageFactory.GetMenuOptionImage(MenuItemOption.VideoLibrary), 1, 24));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.GoToPlaces.LiveTV,
                                        ImageFactory.GetMenuOptionImage(MenuItemOption.TvLive), 2, 16));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.GoToPlaces.TVGuide,
                                        ImageFactory.GetMenuOptionImage(MenuItemOption.TvGuide), 3, 17));

            m_Buttons.Add(new RemoteControlButton(skip, 4, 4));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Back,
                                        ImageFactory.GetMediaImage(MediaIcons.back), 5, 1));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Up,
                                        ImageFactory.GetMediaImage(MediaIcons.arrow_up), 6, 2));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Info,
                                        ImageFactory.GetMediaImage(MediaIcons.info), 7, 3));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.VolumeOptions.Mute,
                                        ImageFactory.GetMediaImage(MediaIcons.mute), 8, 5));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.GoToPlaces.MyPictures,
                                        ImageFactory.GetMenuOptionImage(MenuItemOption.PictureLibrary), 9, 23));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.GoToPlaces.RecordedTV,
                                        ImageFactory.GetMenuOptionImage(MenuItemOption.TvRecorded), 10, 18));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Dvdmenu,
                                        ImageFactory.GetMenuOptionImage(MenuItemOption.DVD), 11, 19));

            m_Buttons.Add(new RemoteControlButton(skip, 12, 11));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Left,
                                        ImageFactory.GetMediaImage(MediaIcons.arrow_left), 13, 6));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Ok,
                                        ImageFactory.GetMediaImage(MediaIcons.ok), 14, 7));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Right,
                                        ImageFactory.GetMediaImage(MediaIcons.arrow_right), 15, 8));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.VolumeOptions.Up,
                                        ImageFactory.GetMediaImage(MediaIcons.vol_up), 16, 10));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Ch_Plus,
                                        ImageFactory.GetMediaImage(MediaIcons.ch_up), 17, 9));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.GoToPlaces.MyMusic,
                                        ImageFactory.GetMenuOptionImage(MenuItemOption.MusicLibrary), 18, 21));

            m_Buttons.Add(new RemoteControlButton(skip, 19, 13));

            m_Buttons.Add(new RemoteControlButton(skip, 20, 20));

            m_Buttons.Add(new RemoteControlButton(skip, 21, 26));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Down,
                                        ImageFactory.GetMediaImage(MediaIcons.arrow_down), 22, 12));

            m_Buttons.Add(new RemoteControlButton(skip, 23, 27));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.VolumeOptions.Down,
                                        ImageFactory.GetMediaImage(MediaIcons.vol_down), 24, 15));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Ch_Minus,
                                        ImageFactory.GetMediaImage(MediaIcons.ch_down), 25, 14));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.GoToPlaces.InternetRadio,
                                        ImageFactory.GetMediaImage(MediaIcons.radio), 26, 22));

            m_Buttons.Add(new RemoteControlButton(skip, 27, 28));

            m_Buttons.Add(new RemoteControlButton(skip, 28, 29));

            m_Buttons.Add(new RemoteControlButton(skip, 29, 31));

            m_Buttons.Add(new RemoteControlButton(skip, 30, 32));

            m_Buttons.Add(new RemoteControlButton(skip, 31, 33));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Skipback,
                                        ImageFactory.GetMediaImage(MediaIcons.player_start), 32, 30));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Rew,
                                        ImageFactory.GetMediaImage(MediaIcons.player_rew), 33, 35));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Stop,
                                        ImageFactory.GetMediaImage(MediaIcons.player_stop), 34, 36));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Play,
                                        ImageFactory.GetMediaImage(MediaIcons.player_play), 35, 37));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Pause,
                                        ImageFactory.GetMediaImage(MediaIcons.player_pause), 36, 38));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Rec,
                                        ImageFactory.GetMediaImage(MediaIcons.player_rec), 37, 25));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Fwd,
                                        ImageFactory.GetMediaImage(MediaIcons.player_fwd), 38, 39));

            m_Buttons.Add(new RemoteControlButton(RemoteCommands.ButtonCommands.Skipfwd,
                                        ImageFactory.GetMediaImage(MediaIcons.player_end), 39, 34));

        }

        public override void OnGesture(GestureEventArgs e)
        {
            if (e.Gesture == Gesture.Right)
            {
                e.Handled = true;
                HideKeyboard();
            }
            else
            {
                base.OnGesture(e);
            }
        }

        public override List<BarOption> GetBarOptions()
        {
            return new List<BarOption>();
        }

        public override void Back()
        {
        }
    }
}
